/*
 * Socket.cpp
 *
 *  Created on: Oct 26, 2012
 *      Author: lucas
 */

#include "Socket.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <iostream>
#include <errno.h>
#include <string.h>

Socket::Socket() {
	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if (sockfd == -1) {
		std::cerr << "Socket::Socket() - ERROR " <<
				errno << ": " << strerror(errno) << std::endl;
		validSocket = false;
	}
	else
		validSocket = true;
}

bool Socket::isValid() {
	return validSocket;
}


Socket::Socket(int sockfd) {
	this->sockfd = sockfd;
	validSocket = true;
}


void Socket::cerrar() {
	if (validSocket) {
		close(sockfd);
		validSocket = false;
	}
}



void Socket::shutDown() {
	shutdown(sockfd, SHUT_RDWR);
}

Socket::~Socket() {
	close(sockfd);
}
